<?php

/**
 * The Home Page
 *
 * @since ADD ADOdb Admin 0.0
 */
CLASS ctrl_page_index EXTENDS ctrl_tpl_adodb_admin {

   public $common_gpc = array(
         '_REQUEST' => array('mode','db','table','page')
      );

   public $mode_gpc_query = array(
         '_REQUEST' => array('sql')
      );

   public $mode_gpc_show_tables = array(
         '_REQUEST' => array('db')
      );

   public $mode_gpc_show_databases = array();

   public $mode_gpc_select_database = array(
         '_REQUEST' => array('database')
      );

   public $data = array(
         'row_actions' => array()
      );

   protected $adodb;

   /**
    * Set the adodb property
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function pre_mode_process($gpc) {
      parent::pre_mode_process($gpc);
      $this->adodb = adodb_user::current_logged_in()->adodb();
   }


   /**
    * Query mode
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function process_mode_query($gpc) {
      extract($gpc);

      $per_page = isset(add::config()->rows_per_page) ? add::config()->rows_per_page : 100;
      try {
         $result = $this->adodb->Execute($sql);
      }
      catch (e_database $e) {
         throw new e_user_input($e->getMessage());
      }

      if (!$result && $this->adodb->ErrorMsg())
         throw new e_user($this->adodb->ErrorMsg());

      $this->assign("rows",$result->getArray(100));
      $this->assign("fields",array_keys((array)$this->data['rows'][0]));

      if (!empty($_REQUEST['ajax'])) {
         $this->table_ajax();
      }
   }


   /**
    * Tables
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function process_mode_show_tables($gpc) {
      extract($gpc);
      $tables = array();
      foreach ($this->adodb->MetaTables() as $table) {
         $tables[] = array('meta_table' => $table);
      }
      if (!$tables && $this->adodb->ErrorMsg())
         throw new e_user($this->adodb->ErrorMsg());


      $this->assign('rows',$tables);
      $this->assign('fields',array('meta_table'));

      if (!empty($_REQUEST['ajax'])) {
         $this->table_ajax();
      }
   }


   /**
    * Tables
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function process_mode_show_databases($gpc) {
      extract($gpc);
      $databases = array();

      foreach ($this->adodb->MetaDatabases() as $database) {
         $databases[] = array('meta_database' => $database);
      }

      if (!$databases && $this->adodb->ErrorMsg())
         throw new e_user($this->adodb->ErrorMsg());


      $this->assign(
            'row_actions',
            array(
               new row_action(
                     array(
                           'inner_html' => 'Select Database',
                           'format'     => array( '?mode=select_database&database=%s','meta_database' )
                        )
                  )
               )
         );

      $this->assign('rows',$databases);

      $this->assign('fields',array('meta_database'));
      if (!empty($_REQUEST['ajax'])) {
         $this->table_ajax();
      }
   }


   /**
    * Select Database
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function process_mode_select_database($gpc) {
      extract($gpc);
      adodb_user::current_logged_in()->database = $database;
      add::redirect('?mode=show_tables');
   }

   /**
    * Response when ajax
    *
    * @since ADD ADOdb Admin 0.0
    */
   public function table_ajax() {
      $json_data = array('type'=>'eval', 'rows' => &$this->data['rows'], 'fields' => &$this->data['fields'], 'row_actions' => &$this->data['row_actions']);

         $json_data['script'] = <<<'EOT'
$table = $('<table><caption>SQL Results</caption><tbody></tbody></table>');
$target.html($table);
$table.append('<thead><tr><th>'+json.fields.join('</th><th>')+'</th></tr></thead>');
if (json.row_actions.length) {
   $table.find('thead tr').prepend('<th colspan="'+json.row_actions.length+'"></th>');
}
$tbody = $table.find('tbody');
for (var x in json.rows) {
   var row = json.rows[x];
   var tr = '<tr>';
   for (var action_name in json.row_actions) {
      var action = json.row_actions[action_name];
      var args = [];

      for ( var a = 1 ; action.format[a]; a++ ) {
         args[args.length] = row[action.format[a]];
      }

      tr += '<td><a href="'+vsprintf(action.format[0],args)+'">'+action.inner_html+'</td>';
   }
   for (var y in json.fields) {
      var field = json.fields[y];
      tr += '<td>'+row[field]+'</td>';
   }
   tr += '</tr>';
   $tbody.append(tr);
}
EOT;

      $this->assign('json_data',$json_data);
   }
}